package gitee.com.simonzhaojia.service;

import gitee.com.simonzhaojia.exception.GetImageUrlFailedException;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 定时下载
 *
 * @author simon zhao
 */
@Configuration
@Slf4j
@Getter
@Setter
public class DownloadBingBGImageService implements ApplicationRunner {


  @Value("${bing.bgsave.bgImageSavePath}")
  private String bgImageSavePath;


  @Override
  public void run(ApplicationArguments args) throws Exception {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
    //定时任务
    executor.scheduleAtFixedRate(new DownLoadTask(this.bgImageSavePath), 0, 1, TimeUnit.DAYS);
    executor.scheduleAtFixedRate(new LogTask(), 0, 10, TimeUnit.MINUTES);
    log.info("DownloadBingBGImage Application start successfully");
  }

  /**
   * 定时打印日志,检测系统是否正常运行
   */
  class LogTask implements Runnable {

    @Override
    public void run() {
      log.info("ThreadPool is running");
    }
  }

}
